package com.iptv.grape;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.bu.IKeyEventPorc;
import com.iptv.grape.bu.factory.BusinessLogicBuilder;
import com.iptv.grape.event.DefBridgeHandler;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.ReadJsonFile;
import com.iptv.grape.util.WebViewUtils;
import com.iptv.grape.video.DefXMediaPlayerEventListener;
import com.iptv.grape.video.XMediaPlayer;

public class DetailActivity extends BaseActivity {

    private BridgeWebView mWebview;
    private String webViewUrl="http://117.158.207.135:8899/index.html#/details/";
    private SurfaceView surfaceView;
    private XMediaPlayer mediaPlayer = null;
    private ProgressBar progressBar;
    // 暂停相关的控件
    private RelativeLayout pauseLayout;
    private DefBridgeHandler webBidgeHandler;
    private DefXMediaPlayerEventListener playerEventListener;


    @Override
    public XMediaPlayer getXMediaPlayer(){
        return mediaPlayer;
    }

    @Override
    public BridgeWebView getBridgeWebView(){
        return this.mWebview;
    }


    public DefXMediaPlayerEventListener getDefXMediaPlayerEventListener(){
        //this.runOnUiThread();
        return this.playerEventListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        initIntent();
        setContentView(R.layout.activity_detail);
        mWebview = findViewById(R.id.bdwebview);
        surfaceView = findViewById(R.id.surfaceView);
        progressBar = findViewById(R.id.progressBar);
        pauseLayout =findViewById(R.id.pause_layout);
        playerEventListener = new DefXMediaPlayerEventListener(this);
        mediaPlayer = new XMediaPlayer(surfaceView,progressBar);
        mediaPlayer.setOnPlayerListener(playerEventListener);

        init();
        GrapeContext.curGrapeContext().registerActivity(this,this.mediaPlayer);
        //topLayout = findViewById(R.id.topLayout);
        //textChannelName = findViewById(R.id.textChannelName);
        //textcurType = findViewById(R.id.textcurType);
        // 注册自己到业务处理对象

    }

    private void initIntent() {
        if (getIntent().getStringExtra("recommendUrl") != null) {
            webViewUrl = getIntent().getStringExtra("recommendUrl");
            AppLog.getCurLog().out("webViewUrl=" + webViewUrl);
        }
    }

    // 初始化Webview
    private void init() {

        //Glide.with(context).load(listRecommend.get(0).get("poster")).into(imgBottom1);

        mWebview.getSettings().setJavaScriptEnabled(true);
        WebViewUtils.initSetting(mWebview,DetailActivity.this,webViewUrl);
        WebViewUtils.initHardwareAccelerate(DetailActivity.this);
        WebViewUtils.initWebView(mWebview,DetailActivity.this,webViewUrl);
        webBidgeHandler = new DefBridgeHandler(this,this.mediaPlayer);
        WebViewUtils.registerHandler(mWebview,webBidgeHandler);
        //mWebview.requestFocus();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册事件
        //EventBus.getDefault().unregister(this);
        ViewParent parent = mWebview.getParent();
        if (parent != null) {
            ((ViewGroup) parent).removeView(mWebview);
        }
        mWebview.stopLoading();
        mWebview.getSettings().setJavaScriptEnabled(false);
        mWebview.clearHistory();
        mWebview.clearView();
        mWebview.removeAllViews();
        mWebview.destroy();
        mWebview = null;
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getAction() !=KeyEvent.ACTION_UP) {
            if(GrapeContext.curGrapeContext().isFocus()) {
                IKeyEventPorc proc = BusinessLogicBuilder.createProc(event);
                if (proc == null) {
                    GrapeContext.curGrapeContext().reSetTimeByAutoClose();
                    return super.dispatchKeyEvent(event);
                } else {
                    proc.onKeyDownEvent(this,this.mediaPlayer, GrapeContext.curGrapeContext(),event);
                    return true;
                }
            }
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void showAdImg(){

        //LinearLayout pauseLayout =findViewById(R.id.pause_layout);
        Bitmap bitmap = ReadJsonFile.getRunImgFile("adimg.jpg");
        if(bitmap != null) {
            pauseLayout.setBackground(new BitmapDrawable(bitmap));
        }else {
            pauseLayout.setBackgroundResource(R.mipmap.splash0);
        }
        pauseLayout.setVisibility(View.VISIBLE);
    }
    @Override
    public void hideAdImg(){
        pauseLayout.setVisibility(View.GONE);
    }
}
